package com.query;


import com.common.ConstantUtil;

import java.util.HashMap;
import java.util.Map;

public abstract class BaseQuery implements IQuery {

    private Integer start = 1;
    private Integer length = ConstantUtil.PAGE_SIZE;
    private String showType = "0";
    private boolean flag;
    protected String sortOrder;
    protected Integer sortField;
    protected Integer sortType;

    protected Map<String, Object> params = new HashMap<>();

    /**
     * 该方法由子类实现，举例：
     * <p/>
     * protected void init() {
     * <p/>
     * Example.Criteria criteria = example.createCriteria();
     * <p/>
     * if (StringUtils.isNotBlank(name)) {
     * <p/>
     * criteria.andLike("name", "%" + name + "%");
     * <p/>
     * }
     * <p/>
     * if (StringUtils.isNotBlank(nameEN)) {
     * <p/>
     * criteria.andEqualTo("age", age);
     * <p/>
     * }
     * <p/>
     * }
     */
    protected abstract void init();

    @Override
    public Integer getStart() {
        if (null == start || start <= 0) {
            start = 1;
        }
        return start;
    }

    public void setStart(Integer start) {
        this.start = start;
    }

    @Override
    public Integer getLength() {
        if (null == length || length <= 0) {
            length = ConstantUtil.PAGE_SIZE;
        }
        return length;
    }

    public void setLength(Integer length) {
        this.length = length;
    }

    public String getShowType() {
        return showType;
    }

    public void setShowType(String showType) {
        this.showType = showType;
    }

    public void sortOrder(String sortOrder) {
        this.sortOrder = sortOrder;
    }

    public Integer getSortField() {
        return sortField;
    }

    public void setSortField(Integer sortField) {
        this.sortField = sortField;
    }

    public Integer getSortType() {
        return sortType;
    }

    public void setSortType(Integer sortType) {
        this.sortType = sortType;
    }

    @Override
    public Map<String, Object> params() {
        if (!flag) {
            init();
            flag = true;
        }
        return params;
    }
}
